package com.extrace;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.maps.model.Polyline;
import com.amap.api.maps.model.PolylineOptions;
import com.extrace.loader.PackageRouteLoader;
import com.extrace.model.PackageRoute;
import com.extrace.net.IDataAdapter;

import java.util.ArrayList;
import java.util.List;

public class Map_Activity extends AppCompatActivity implements IDataAdapter<List<PackageRoute>> {

    private EditText ExpressId;
    private Button btnByList;
    private Button btnByMap;

    private MapView mMapView = null;
    private AMap aMap = null;
    private Polyline polyline;
    private List<PackageRoute> list;
    private Intent mIntent;
    private PackageRouteLoader mLoader;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map_activity);

                        //获取地图控件引用
        mMapView = (MapView) findViewById(R.id.mapView);
        //在activity执行onCreate时执行mMapView.onCreate(savedInstanceState)，创建地图
        mMapView.onCreate(savedInstanceState);

        mIntent = this.getIntent();
        if (mIntent.hasExtra("expressId")){
            LoadPackageRoute(mIntent.getStringExtra("expressId"));
        }

        ExpressId = (EditText)findViewById(R.id.expressId);

        btnByList = (Button) findViewById(R.id.btnByList);
        btnByList.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mIntent.putExtra("expressId", ExpressId.getText().toString());
                finish();
            }
        });

        btnByMap = (Button) findViewById(R.id.btnByMap);
        btnByMap.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LoadPackageRoute(ExpressId.getText().toString());
            }
        });

    }

    public void LoadPackageRoute(String exId) {
        try {
            mLoader = new PackageRouteLoader(this, this);
            mLoader.getPackageRoute(exId);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void RefreshMap() {
        //初始化地图控制器对象
        if (aMap == null) {
            aMap = mMapView.getMap();
        }

        //尝试绘制点
        if (list.size() != 0) {
            LatLng latLng = new LatLng(list.get(0).getX(),list.get(0).getY());
            final Marker marker = aMap.addMarker(new MarkerOptions().position(latLng).title("3303").snippet("机房教室所在位置"));

            LatLng latLng_silent = new LatLng(list.get(0).getX(),list.get(0).getY());//构造一个初始位置(钟楼)
            aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng_silent,    17));//v是比例尺
//尝试绘制路线
            List<LatLng> latLngs = new ArrayList<LatLng>();
            for (PackageRoute pr : list) {
                latLngs.add(new LatLng(pr.getX(), pr.getY()));
            }
            polyline =aMap.addPolyline(new PolylineOptions().useGradient(true).
                    addAll(latLngs).width(10));
        } else {
            Toast.makeText(getApplicationContext(),"没有快件转运记录！", Toast.LENGTH_SHORT).show();
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //在activity执行onDestroy时执行mMapView.onDestroy()，销毁地图
        mMapView.onDestroy();
    }
    @Override
    protected void onResume() {
        super.onResume();
        //在activity执行onResume时执行mMapView.onResume ()，重新绘制加载地图
        mMapView.onResume();
    }

    @Override
        protected void onPause() {
        super.onPause();
    //在activity执行onPause时执行mMapView.onPause ()，暂停地图的绘制
        mMapView.onPause();
    }
    @Override
    protected void onSaveInstanceState(Bundle outState) {
            super.onSaveInstanceState(outState);
            //在activity执行onSaveInstanceState时执行mMapView.onSaveInstanceState (outState)，保存地图当前的状态
             mMapView.onSaveInstanceState(outState);
    }


    @Override
    public List<PackageRoute> getData() {
        return list;
    }

    @Override
    public void setData(List<PackageRoute> data) {
        list = data;
    }

    @Override
    public void notifyDataSetChanged() {
        RefreshMap();
    }
}
